package ma.android.oook;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;

public class OookPreferences extends PreferenceActivity implements OnPreferenceChangeListener{

	private static final String OOOK_PREF_FILE="OookFile";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);

		ListPreference currencyPreference = (ListPreference) findPreference(getString(R.string.currency));
		currencyPreference.setOnPreferenceChangeListener(this);
        
        ListPreference languagePreference = (ListPreference) findPreference(getString(R.string.language));
        languagePreference.setOnPreferenceChangeListener(this);
        
        ListPreference dateTypePreference = (ListPreference) findPreference(getString(R.string.dateType));
        dateTypePreference.setOnPreferenceChangeListener(this);
        
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		SharedPreferences preferences = getSharedPreferences(OOOK_PREF_FILE, MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		if (preference.getKey().equals(getString(R.string.language)))
			editor.putString(preference.getKey(), String.valueOf(newValue));
		else if (preference.getKey().equals(getString(R.string.currency)))
		{
			editor.putString(preference.getKey(), String.valueOf(newValue));
			editor.putString(getString(R.string.currencySymbol),OookUtils.getCurrencySymbol(String.valueOf(newValue)));
		}
		else if (preference.getKey().equals(getString(R.string.dateType)))
			editor.putInt(preference.getKey(), Integer.valueOf(String.valueOf(newValue)));
		editor.commit();
		return false;
	}
}
